from spans import get_spans
from log import get_log
import os
from get_metrics import get_metric_data
from datetime import datetime, timedelta




def main():
    # 创建 metrics 目录
    os.makedirs("data", exist_ok=True)

    # 修改采集日期 北京时间
    start_time_8 = "2025-05-27T12:00:00"
    end_time_8 = "2025-05-27T12:13:59"

    # 转还为十位时间戳
    tmptime_start = datetime.strptime(start_time_8, "%Y-%m-%dT%H:%M:%S")
    tmptime_end = datetime.strptime(end_time_8, "%Y-%m-%dT%H:%M:%S")
    tmptime_start = str(int(tmptime_start.timestamp()))
    tmptime_end = str(int(tmptime_end.timestamp()))
    print(f"开始时间: {tmptime_start}, 结束时间: {tmptime_end}")

    # 生成数据目录
    # 目录名
    folder_name = "TT." + start_time_8.replace(':','') + "D" + end_time_8.replace(':','')
    folder_path = os.path.join("data", folder_name)
    os.makedirs(folder_path, exist_ok=True)

    # 提取 logs
    # 转换时间 东八区转换为 UTC 时间
    start_time = str(datetime.strptime(start_time_8, "%Y-%m-%dT%H:%M:%S") - timedelta(hours=8) ).replace(" ", "T")
    end_time = str(datetime.strptime(end_time_8, "%Y-%m-%dT%H:%M:%S") - timedelta(hours=8)).replace(" ", "T")
    get_log(start_time, end_time, folder_path)

    # 提取 spans
    # 转换时间 
    # START_TIME = "2025-05-22 0537"
    # END_TIME = "2025-05-22 0552"
    START_TIME = start_time.replace("T", " ").replace(":", "")[:-2]
    END_TIME = end_time.replace("T", " ").replace(":", "")[:-2]
    # print(f"开始提取 spans: {START_TIME} - {END_TIME}")
    get_spans(START_TIME, END_TIME, folder_path)

    # 提取 metrics
    # 转换时间
    # start_time = datetime.strptime("2025-05-25 17:00:00", "%Y-%m-%d %H:%M:%S")
    # end_time = datetime.strptime("2025-05-25 17:30:00", "%Y-%m-%d %H:%M:%S")
    start_time = start_time_8.replace("T", " ")
    end_time = end_time_8.replace("T", " ")
    start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
    end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")

    # print(f"开始提取 metrics: {start_time} - {end_time}")
    get_metric_data(start_time, end_time, folder_path, tmptime_start, tmptime_end)



if __name__ == "__main__":
    main()
